#include "changepassworddialog.h"
#include "libs/controls/toastlabel.h"
#include "services/yingceservice.h"
#include "ui_changepassworddialog.h"
#include "yingcewindow.h"

ChangePasswordDialog::ChangePasswordDialog(QWidget* parent)
    : QDialog(parent), ui(new Ui::ChangePasswordDialog)
{
    setWindowTitle(tr("Change Password"));
    ui->setupUi(this);
    setTabOrder({ ui->current_edit, ui->new_edit, ui->confirm_edit });
    ui->current_edit->setFocus();

    connect(ui->current_edit, &QLineEdit::textChanged, this, &ChangePasswordDialog::updateUiStatus);
    connect(ui->new_edit, &QLineEdit::textChanged, this, &ChangePasswordDialog::updateUiStatus);
    connect(ui->confirm_edit, &QLineEdit::textChanged, this, &ChangePasswordDialog::updateUiStatus);

    connect(ui->current_button, &QPushButton::toggled, this, [this](bool checked) {
        ui->current_edit->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
    });
    connect(ui->new_button, &QPushButton::toggled, this, [this](bool checked) {
        ui->new_edit->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
    });
    connect(ui->confirm_button, &QPushButton::toggled, this, [this](bool checked) {
        ui->confirm_edit->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
    });

    connect(ui->ok_button, &QPushButton::clicked, this, [this] {
        const QString current = ui->current_edit->text().trimmed();
        const QString new_pwd = ui->new_edit->text().trimmed();
        const QString md5_current = QString::fromUtf8(
                QCryptographicHash::hash(current.toUtf8(), QCryptographicHash::Md5).toBase64());
        const QString md5_new_pwd = QString::fromUtf8(
                QCryptographicHash::hash(new_pwd.toUtf8(), QCryptographicHash::Md5).toBase64());
        YingCeService::instance()->changePassword(
                md5_current, md5_new_pwd,
                yc::ResponseHandler(
                        this, [this](const yc::Request& request, const yc::Response& response) {
                            if (response.status() == yc::OK_CODE) {
                                yc::ToastLabel::showMessage(tr("Success"),
                                                            YingCeWindow::instance());
                                this->accept();
                            } else {
                                yc::ToastLabel::showMessage(
                                        tr("Failed to change the password, error:%1.")
                                                .arg(response.msg()),
                                        this);
                            }
                        }));
    });
    connect(ui->cancel_button, &QPushButton::clicked, this, &QDialog::reject);
}

ChangePasswordDialog::~ChangePasswordDialog()
{
    delete ui;
}

void ChangePasswordDialog::updateUiStatus()
{
    const QString current = ui->current_edit->text().trimmed();
    const QString new_pwd = ui->new_edit->text().trimmed();
    const QString confirm_pwd = ui->confirm_edit->text().trimmed();
    ui->ok_button->setEnabled(!current.isEmpty() && !new_pwd.isEmpty() && new_pwd == confirm_pwd);
}
